#!/bin/python
#-*- coding:utf-8 -*-
# Filename:    main.py
# Revision:    1.0
# Date:        2012-06-14
# Author:      simonzhang
# web:         www.simonzhang.net
# Email:       simon-zzm@163.com
### END INIT INFO
import sys
import hashlib
import tornado.ioloop
import tornado.locale
import tornado.web
from dbmodel import *
from interface import *


class WebInfoHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        _get_user = self.check_user()
        _domain = self.get_argument("textfield")
        _title = self.get_argument("textfield2")
        insert_global_webinfo(_domain, _title)
        self.redirect('/index/global_set/')

class AddUserHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        _get_user = self.check_user()
        _domain, _title = if_get_domain_title()
        _get_user_left_menu = if_get_user_left_menu(_get_user)
        _user = self.get_argument("textfield3")
        _get_db_user_id = get_db_user_info(_user)
        if len(_get_db_user_id) == 0:
            try:
                _tmp_passwd = self.get_argument("textfield4")
            except:
                _tmp_passwd = "123456"
            _passwd = hashlib.md5("%s+%s" % (_user, _tmp_passwd)).hexdigest()
            try:
                _email = self.get_argument("textfield5")
            except:
                _email = "NULL"
            add_user_info(_user, _passwd, _email)
            self.redirect('/index/global_set/')
        else:
            self.render("templates/mycontext.html", domain=_domain, \
                        title=_title, username = _get_user, \
                        items=_get_user_left_menu, \
                        error_context=["User name is use."])

class InitPasswdHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        _get_user = self.check_user()
        _domain, _title = if_get_domain_title()
        _get_user_left_menu = if_get_user_left_menu(_get_user)
        _user = self.get_argument("u31")
        _get_db_status = idb_init_user_passwd(_user, \
                                           hashlib.md5("%s+1234567" % _user).hexdigest()) 
        if _get_db_status == "OK":
            self.render("templates/mycontext.html", domain=_domain, \
                         title=_title, username = _get_user, \
                         items=_get_user_left_menu, \
                         error_context=["修改成功"])
        else:
            self.render("templates/mycontext.html", domain=_domain, \
                         title=_title, username = _get_user, \
                         items=_get_user_left_menu, \
                         error_context=["修改失败"])

class DisbleUserHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        _get_user = self.check_user()
        _domain, _title = if_get_domain_title()
        _get_user_left_menu = if_get_user_left_menu(_get_user)
        _user = self.get_argument("u32")
        _get_db_status = idb_init_user_passwd(_user, "disble")
        if _get_db_status == "OK":
            self.render("templates/mycontext.html", domain=_domain, \
                         title=_title, username = _get_user, \
                         items=_get_user_left_menu, \
                         error_context=["修改成功"])
        else:
            self.render("templates/mycontext.html", domain=_domain, \
                         title=_title, username = _get_user, \
                         items=_get_user_left_menu, \
                         error_context=["修改失败"])

class AddProjectHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        _get_user = self.check_user()
        _domain, _title = if_get_domain_title()
        _get_user_left_menu = if_get_user_left_menu(_get_user)
        _project_name = self.get_argument("textfield_pro1")
        _project_remark = self.get_argument("textfield_pro2")
        _now_have_project_name_list = []
        for _tmp in get_project_list_all():
            _now_have_project_name_list.append(_tmp["project"])
        if _project_name not in _now_have_project_name_list:
            add_project(_project_name, _project_remark)
            self.redirect('/index/global_set/')
        else:
            self.render("templates/mycontext.html", domain=_domain, \
                         title=_title, username = _get_user, \
                         items=_get_user_left_menu, \
                         error_context=["已经有相同名字的项目"])



class EditAuthHandler(BaseHandler):
    def get(self):
        _get_user = self.check_user()
        _get_user_name = self.get_argument('u')
        _get_project_list = ",".join(self.get_arguments('checkboxproject'))
        _get_menu_list = ",".join(self.get_arguments('checkboxmenu'))
        edit_user_auth(_get_user_name, _get_project_list, _get_menu_list)
        self.redirect('/index/global_set/')

class GetAuthHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        _get_user = self.check_user()
        _get_user = self.get_argument('u')
        _get_user_left_menu_id_list = if_get_user_left_menu(_get_user)
        _get_user_manage_project_id_list = if_get_user_project(_get_user)
        _get_all_project_id_list = get_project_list_all()
        _get_all_menu_id_list = get_left_menu_list_all()
        self.render("templates/getauth.html", projects = _get_all_project_id_list, \
                                              menus = _get_all_menu_id_list, \
                                              cproject = _get_user_manage_project_id_list, \
                                              cmenu = _get_user_left_menu_id_list)

class GlobalSetHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        _get_user = self.check_user()
        _domain, _title = if_get_domain_title()
        _get_user_left_menu = if_get_user_left_menu(_get_user)
        _get_user_manage_project = if_get_user_project(_get_user)
        _get_user_name = get_all_user_name()
        self.render("templates/global_set.html",  domain=_domain, \
                                                  title=_title, \
                                                  username = _get_user, \
                                                  items = _get_user_left_menu, \
                                                  users=_get_user_name, \
                                                  pros=_get_user_manage_project)
    def post(self):
        self.clear_all_cookies()
        self.redirect("/")

